/**
 * 开发环境Mock服务
 * 当后端服务不可用时提供模拟数据
 */

const mockData = {
  // 首页轮播图
  banners: [
    {
      id: 1,
      title: '智慧社区欢迎您',
      image: '/images/banner1.jpg',
      url: '/pages/community/index'
    },
    {
      id: 2,
      title: '物品交换平台',
      image: '/images/banner2.jpg',
      url: '/pages/swap/index'
    },
    {
      id: 3,
      title: '寻找志同道合的伙伴',
      image: '/images/banner3.jpg',
      url: '/pages/buddy/index'
    }
  ],

  // 热门分类
  categories: [
    {
      id: 1,
      name: '社区服务',
      icon: '/images/icons/community.png',
      url: '/pages/community/index',
      color: '#10b981'
    },
    {
      id: 2,
      name: '物品交换',
      icon: '/images/icons/swap.png',
      url: '/pages/swap/index',
      color: '#f59e0b'
    },
    {
      id: 3,
      name: '搭子活动',
      icon: '/images/icons/buddy.png',
      url: '/pages/buddy/index',
      color: '#3b82f6'
    },
    {
      id: 4,
      name: '教师服务',
      icon: '/images/icons/teacher.png',
      url: '/pages/teacher-main/index',
      color: '#8b5cf6'
    }
  ],

  // 模拟用户数据
  user: {
    id: 1,
    nickname: '测试用户',
    avatar: '/images/avatar/default.png',
    phone: '13800138000'
  }
};

class MockService {
  constructor() {
    this.isEnabled = true; // 开发环境下启用mock
    this.delay = 500; // 模拟网络延迟
  }

  /**
   * 模拟API响应
   */
  mockResponse(data, success = true) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (success) {
          resolve({
            code: 0,
            message: 'success',
            data: data
          });
        } else {
          reject(new Error('Mock error'));
        }
      }, this.delay);
    });
  }

  /**
   * 获取首页数据
   */
  getHomeData() {
    return this.mockResponse({
      banners: mockData.banners,
      categories: mockData.categories,
      recommends: []
    });
  }

  /**
   * 获取用户信息
   */
  getUserInfo() {
    return this.mockResponse(mockData.user);
  }

  /**
   * 获取分类列表
   */
  getCategories() {
    return this.mockResponse(mockData.categories);
  }

  /**
   * 搜索（模拟）
   */
  search(keyword, type = 'all') {
    return this.mockResponse({
      results: [],
      total: 0,
      keyword: keyword,
      type: type
    });
  }

  /**
   * 检查是否应该使用mock
   */
  shouldUseMock() {
    return this.isEnabled && typeof wx !== 'undefined' && wx.getSystemInfoSync;
  }
}

const mockService = new MockService();

module.exports = mockService;